﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class NetUnionFightGuard : NetUnionFightBase
    {
        public Dictionary<long, InstUFGuard> guards = new Dictionary<long, InstUFGuard>();

        public NetUnionFightGuard(RoomUnionFight unionFight, RoomCharactorManager mgr, RoomNetUnionFight net)
            :base(unionFight,mgr,net)
        {
        }

        public override void Enter()
        {
            base.Enter();
            guards.Clear();
        }

        public override void Leave()
        {
            base.Leave();
            guards.Clear();
        }

        public override RoomCharactor CreateCharactor(long uuid)
        {
            var instData = guards[uuid];
            charactorManager.CreateUnionGuard(guards[uuid]);
            guards[uuid] = instData;
            return charactorManager.GetCharactor(uuid+"");
        }

        public override void ClearCharactorData(long uuid)
        {
            base.ClearCharactorData(uuid);

            if (guards.ContainsKey(uuid))
                guards.Remove(uuid);
        }

        public void GuardRefresh(InstUFGuard msg)
        {

            if (msg == null)
            {
                return;

            }
            //var dictData = Dict.Blo.DictUnionFightGuardBlo.GetByID(msg.UFGuardId+"");
            var uuid = msg.guid;
            guards[uuid] = msg;
            //var pos  = Dict.Blo.DictUnionFightGuardBlo.GetPosition(msg.UFGuardId + "");
            var charactor = GetCharactor(uuid,false);
            if(charactor==null)
            {
                charactor = CreateCharactor(uuid);
            }
            states[uuid]= msg.state;
            

            if(states[uuid] == (int)UnionGuardState.Death)
            {
                UpdateState(uuid, msg.state, null);

                var bindCharactor = GetCharactor(msg.bindGuid);
                if (bindCharactor != null)
                {
                    if (bindCharactor.type == CharactorType.UnionFightCamp)
                    {
                        //do nothing
                    }
                    else if (bindCharactor.type == CharactorType.UnionFightChariotFactory)
                    {
                        UpdateState(msg.bindGuid, (int)UnionChariotFactoryState.Unactived);
                    }
                    else if (bindCharactor.type == CharactorType.UnionFightTower)
                    {
                        UpdateState(msg.bindGuid, (int)UnionTowerState.Unactived);
                    }
                }
            }
            else
            {
                UpdateState(uuid, msg.state);

                var bindCharactor = GetCharactor(msg.bindGuid);
                if (bindCharactor != null)
                {
                    if (bindCharactor.type == CharactorType.UnionFightCamp)
                    {
                        //do nothing
                    }
                    else if (bindCharactor.type == CharactorType.UnionFightChariotFactory)
                    {
                        UpdateState(msg.bindGuid, (int)UnionChariotFactoryState.Actived);
                    }
                    else if (bindCharactor.type == CharactorType.UnionFightTower)
                    {
                        UpdateState(msg.bindGuid, (int)UnionTowerState.Actived);
                    }
                }
            }

            Debug.Log("uuid=" + uuid + " " + "[GUARD state]=[" + (UnionGuardState)msg.state + "]");

            var pos = new Vector3(msg.initPosition.x, msg.initPosition.y, msg.initPosition.z);
            UpdatePosition(uuid, pos);
            UpdateHp(msg.fightInfo);
        }

        
        /// <summary>
        /// 守卫战斗开始
        /// </summary>
        /// <param name="msg"></param>
        public void GuardFightBegin(SCUFGuardFightBegin msg)
        {
            var uuid = msg.guid;
            var state = (int)UnionGuardState.Attack;
            UpdateState(uuid, state);
        }

        /// <summary>
        /// 守卫战斗结束
        /// </summary>
        /// <param name="msg"></param>

        public void GuardFightEnd(SCUFGuardFightEnd msg)
        {
            var uuid = msg.guid;
            guards[uuid].fightInfo = msg.fightSide;
            var state = (int)UnionGuardState.Stand;
            UpdateState(uuid, state);

            UpdateHp(msg.fightSide);
        }
        
        /// <summary>
        /// 守卫死亡
        /// </summary>
        /// <param name="msg"></param>
        public void GuardDead(SCUFGuardDead msg)
        {
            var uuid = msg.guid;
            if (guards.ContainsKey(uuid))
            {
                var guard = guards[uuid];
                var bindGurad = guard.bindGuid;
                var bindCharactor = GetCharactor(bindGurad);
                if(bindCharactor!=null)
                {
                    if(bindCharactor.type == CharactorType.UnionFightTower)
                    {
                        UpdateState(bindGurad, (int)UnionTowerState.Unactived);
                    }
                    if(bindCharactor.type == CharactorType.UnionFightChariotFactory)
                    {
                        UpdateState(bindGurad, (int)UnionChariotFactoryState.Unactived);
                    }
                }
            }
            var charactor = GetCharactor(uuid);
            if (charactor != null)
            {
                var ai = charactor.ai as RCAIGuard;
                var state = (int)UnionGuardState.Death;
                UpdateState(uuid, state);
            }
        }
    }


}